package com.itheima.test;

import com.itheima.test.utils.JedisUtil;
import org.testng.annotations.Test;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

/*
异常信息1：Caused by: java.net.ConnectException: Connection refused (Connection refused)或者connect timed out
   可能的原因：redis-server是否开启，防火墙是否关闭  systemctl status firewalld
                关闭防火墙：systemctl stop firewalld
   真正的原因：redis.conf文件中定义了只能本机连接redis服务器，不能远程连接。内部有一句 bind 127.0.0.1
   解决：将redis.conf文件中的bind 127.0.0.1注释掉，重启redis-server

异常信息2：redis.clients.jedis.exceptions.JedisDataException:DENIED Redis is running in protected mode because protected mode is enabled,no bind address was specified, no authentication password is requested to clients.
   原因：注释了redis.conf文件中的bind 127.0.0.1，redis默认启动了保护模式，需要客户端提供密码才可以访问。重启redis-server
   解决：修改redis.conf文件，搜索关键字：底行模式输入:/requirepass，设置密码，然后客户采用设置的密码来连接

其他连接被拒绝的原因：linux的防火墙拦截了。
            查看防火墙启动状态：systemctl status firewalld
            关闭防火墙：systemctl stop firewalld
  */
public class Demo1 {
    @Test
    public void test1(){
        //【第一步】创建Jedis对象
        //Jedis jedis=new Jedis(); //默认连接本级6379端口的redis服务器
        Jedis jedis=new Jedis("192.168.222.129",6379);
        //设置连接密码
        jedis.auth("123456");

        //【第二步】执行操作
        //存值
        jedis.set("username","jack");
        //取值
        String username = jedis.get("username");
        System.out.println("username = " + username);

        //【第三步】释放资源
        jedis.close();
    }

    @Test
    public void test(){
        Jedis jedis = new Jedis("192.168.222.129",6379);
        jedis.auth("123456");

        jedis.mset("age","123","address","wuhan");
        List<String> list = jedis.mget("age", "address");
            System.out.println(list);
        jedis.close();
    }

    @Test
    public void test4(){
        Jedis jedis = new Jedis("192.168.222.129", 6379);
        jedis.auth("123456");

        jedis.lpush("list1","a","b","c","d","e");
        List<String> list = jedis.lrange("list1", 0, -1);
        System.out.println(list);
        jedis.close();
    }

    @Test
    public void testList(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.rpush("list2","a","b","c","d","e");
        List<String> list2 = jedis.lrange("list2", 0, -1);
        System.out.println(list2);
        JedisUtil.close(jedis);
    }

    @Test
    public void testHash(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.hset("user","name","zhangsan");
        jedis.hset("user","age","555");
        String name = jedis.hget("user", "name");
        Set<String> user = jedis.hkeys("user");
        List<String> user1 = jedis.hvals("user");
        Map<String, String> user2 = jedis.hgetAll("user");
        System.out.println("所有键为"+user);
        System.out.println("所有值为"+user1);
        System.out.println(name);
        System.out.println("所有键值对为"+user2);
        Long user3 = jedis.hlen("user");
        System.out.println("长度为"+user3);
        jedis.close();
    }

    @Test
    public void testSet(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.sadd("user1","a","a1","b","b1","c","c1","d","d1","e","e1");
        Set<String> user1 = jedis.smembers("user1");
        System.out.println("所有值为"+user1);
        Boolean sismember = jedis.sismember("user1", "b");
        System.out.println(sismember);
        Long user11 = jedis.scard("user1");
        System.out.println("长度为"+user11);
        List<String> user12 = jedis.srandmember("user1", 3);
        System.out.println("随机取三个值为"+user12);
        JedisUtil.close(jedis);
    }

    @Test
    public void key(){
        Jedis jedis = JedisUtil.getJedis();
        Set<String> keys = jedis.keys("*");
       // jedis.expire("age",50);
        Long age = jedis.ttl("age");
        System.out.println("age的时效为："+age);
        jedis.persist("user");
        Long user = jedis.ttl("user");
        System.out.println("user的时效为"+user);
        System.out.println(keys);
        JedisUtil.close(jedis);
    }
}
